/**
 * 享元模式, 一个绘图工具的实现，共享内部形状及颜色
 */

// 绘图接口
class Shape {}

Shape.prototype.draw = () => {}

// 圆形
class Circle extends Shape{
  constructor(color) {
    super() 
    this.color = color
  }

  draw() {
    console.log(`这是一个${this.color}圆形`)  
  }

}

// 工厂类
class FlyweightFactory {
  constructor(color) {
    this.map = new Map()
    if(this.map.get(color)) return this.map.get(color)
    this.shape = new Circle(color)
    this.map.set(color, this.shape)
    return this.shape
  }

}

const whiteShape = new FlyweightFactory('白色')
const blackShape = new FlyweightFactory('黑色')
const redShape = new FlyweightFactory('红色')
const blueShape = new FlyweightFactory('蓝色')

whiteShape.draw()
blackShape.draw()
redShape.draw()
blueShape.draw()
